#include "modelproposala.h"
#include "backendfiles/listmaintence.h"
#include "backendfiles/database.h"
ModelproposalA::ModelproposalA(QObject *parent) :
    QAbstractListModel(parent)
{
    connect(DataBase::getInstance(),&DataBase::baseReady,this,&ModelproposalA::fillModel);
    //connect(DataBase::getInstance(),&DataBase::proposalReturn,this,&ModelproposalA::fillModel);
}
QVariant ModelproposalA::data(const QModelIndex &index, int role) const
{
    if (index.row() < 0 || index.row() > dataList.length())
        return QVariant();
    if(role == ItemID)
        return dataList[index.row()].value("item");
    if(role == ItemIcon)
        return assistDataList.value(dataList[index.row()].value("item")).value("icon");//сурово да
    if(role == ItemName)
        return assistDataList.value(dataList[index.row()].value("item")).value("name");//сурово да
    if(role == ItemBid)
        return dataList[index.row()].value("bid");
    if(role == ItemBayout)
        return dataList[index.row()].value("buyout");
    if(role == ItemQuntity)
        return dataList[index.row()].value("quantity");
    if(role == ItemTimeleft)
        return dataList[index.row()].value("timeLeft");
    if(role == ItemRand)
        return dataList[index.row()].value("rand");
    if(role == ItemSeed)
        return dataList[index.row()].value("seed");
    //if(role == Item)
    //  return list[index.row()][0];
    // "VALUES (:item,:bid,:buyout,:quantity,:timeLeft,:rand,:seed)");
}

int ModelproposalA::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return dataList.size();

}

void ModelproposalA::fillModel()
{

    qDebug()<<"fill model";
    dataList = DataBase::getInstance()->_proposalInCollection(ListMaintence::getInstance()->collectionListContent());
    assistDataList = ListMaintence::getInstance()->collectionAssistList();
    beginResetModel();
    endResetModel();
}

QHash<int, QByteArray> ModelproposalA::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[ItemID] = "id";
    roles[ItemIcon] = "icon";
    roles[ItemName] = "name";
    roles[ItemBid] = "bid";
    roles[ItemBayout] = "buyout";
    roles[ItemQuntity] = "quantity";
    roles[ItemTimeleft] = "timeLeft";
    roles[ItemRand] = "rand";
    roles[ItemSeed] = "seed";
    return roles;
}

void ModelproposalA::refresh()
{
    fillModel();
}
